package com.lizhaoshan.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.lizhaoshan.common.TokenHolder;
import com.lizhaoshan.dao.OrdersDao;
import com.lizhaoshan.dto.OrdersDto;
import com.lizhaoshan.pojo.Orders;
import com.lizhaoshan.pojo.PageBean;
import com.lizhaoshan.service.OrdersService;
import com.lizhaoshan.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: reggie
 * @description:
 * @author: lamour
 * @create: 2022-05-14 17:19
 **/

@Slf4j
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;


    @Override
    public boolean updateOrder(OrdersDto ordersDto) {

        log.info("**********修改订单服务层，begin");
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersDto, orders);

        log.info("**********修改订单服务层，end");
        return ordersDao.updateOrders(orders) > 0 ;
    }

    @Override
    public PageBean<OrderVo> getPagesOrders(Integer pageSize, Integer page, String number, String beginTime, String endTime) {
        int begin = (page-1) * pageSize;
        int size = pageSize;

        List<Orders> ordersList = ordersDao.selectPage(size, begin, number, beginTime, endTime);

        if (CollUtil.isEmpty(ordersList)){
            return null;
        }

        ArrayList<OrderVo> orderVos = new ArrayList<>();

        for (Orders orders : ordersList) {
            if (Objects.nonNull(orders)){
                OrderVo orderVo = new OrderVo();
                BeanUtils.copyProperties(orders,orderVo);
                orderVos.add(orderVo);
            }
        }


        log.info("**********统计条目数，begin");
        int countTotal = ordersDao.countTotal(TokenHolder.getCurrentId());
        log.info("**********统计条目数，end");

        PageBean<OrderVo> orderVoPageBean = new PageBean<>();
        orderVoPageBean.setRecords(orderVos);
        orderVoPageBean.setTotal(countTotal);

        return orderVoPageBean;
    }
}
